﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class RulerControl : UserControl
    {
        private double _width = 10000;
        public bool IsVertical { get; set; }
        public RulerControl()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MyrulerControl_Loaded);
        }

        void MyrulerControl_Loaded(object sender, RoutedEventArgs e)
        {
            AddLine();
        }
        public void SetWidth(double width)
        {   
             if (IsVertical) LayoutRoot.Height = width;
             else LayoutRoot.Width = width;
            _width = width;
            AddLine();
        }

        public void AddLine()
        {
            int count = 0;
            LayoutRoot.Children.Clear();
            if (IsVertical)
            {
                for (int i = 0; i <= _width; i = i + 50)
                {
                    if (i == 0 || i % 50 == 0)
                    {
                        Line l = new Line
                        {
                            Stroke = new SolidColorBrush(Colors.Black),
                            Y1 = i,
                            X1 = this.Width - this.Width / 2,
                            Y2 = i,
                            X2 = this.Width - 2 - this.Width / 2
                        };
                        LayoutRoot.Children.Add(l);

                        TextBlock tb = new TextBlock();
                        tb.Text = i.ToString();
                        tb.FontSize = 9;
                        tb.SetValue(Canvas.TopProperty, (double)(i - 15));
                        tb.SetValue(Canvas.LeftProperty, 10.5);
                        LayoutRoot.Children.Add(tb);
                        count++;

                    }

                    else if (i % 50 == 10)
                    {
                        Line l = new Line
                        {
                            Stroke = new SolidColorBrush(Colors.Black),
                            Y1 = i,
                            X1 = this.Width - 2,
                            Y2 = i,
                            X2 = this.Width - 2 - this.Width / 4
                        };
                        LayoutRoot.Children.Add(l);
                    }
                }

            }
            else
            {
                for (int i = 0; i <= _width; i = i + 50)
                {
                    if (i == 0 || i % 50 == 0)
                    {
                        Line l = new Line
                        {
                            Stroke = new SolidColorBrush(Colors.Black),
                            X1 = i,
                            Y1 = this.Height - this.Height / 2,
                            X2 = i,
                            Y2 = this.Height - 2 - this.Height / 2
                        };
                        LayoutRoot.Children.Add(l);

                        TextBlock tb = new TextBlock();
                        tb.Text = i.ToString();
                        tb.FontSize = 9;
                        tb.SetValue(Canvas.LeftProperty, (double)(i - 10));
                        tb.SetValue(Canvas.TopProperty, 15.5);
                        LayoutRoot.Children.Add(tb);
                        count++;

                    }

                    else if (i % 50 == 10)
                    {
                        Line l = new Line
                        {
                            Stroke = new SolidColorBrush(Colors.Black),
                            X1 = i,
                            Y1 = this.Height - 2,
                            X2 = i,
                            Y2 = this.Height - 2 - this.Height / 4
                        };
                        LayoutRoot.Children.Add(l);
                    }
                }
            }
        }
    }
}
